package com.zat.mes.andrmes.customerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.zat.mes.andrmes.R;


/**
 * 自带边框的布局容器
 */
public class DrawLineTextView extends TextView {
    /**
     * 左侧
     */
    public static final int GRAVITY_LEFT = 0;
    /**
     * 右侧
     */
    public static final int GRAVITY_RIGHT = 1;
    /**
     * 顶部
     */
    public static final int GRAVITY_TOP = 2;
    /**
     * 底部
     */
    public static final int GRAVITY_BOTTOM = 3;
    /**
     * 全部
     */
    public static final int GRAVITY_ALL = 4;
    /**
     * 顶部 底部
     */
    public static final int GRAVITY_TOP_BOTTOM = 5;
    /**
     * 左侧底部
     */
    public static final int GRAVITY_LEFT_BOTTOM = 6;
    /**
     * 左侧 底部 右侧
     */
    public static final int GRAVITY_LEFT_BOTTOM_RIGHT = 7;
    /**
     * 顶部 左侧 底部
     */
    public static final int GRAVITY_TOP_LEFT_BOTTOM = 8;
    private Paint paint;
    /**
     * 线条颜色
     */
    private int lineColor;
    /**
     * 线条宽度
     */
    private float lineWidth;
    /**
     * 线条方向
     */
    private int lineGravity;
    /**
     * 是否处理焦点事件
     */
    private boolean dealOnTouch;
    private int width;
    private int height;
    private Context context;
    private float linePaddingLeft;
    private float linePaddingRight;
    private float linePaddingTop;
    private float linePaddingBottom;

    public DrawLineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    /**
     * 读取XML
     *
     * @param attrs
     */
    private void readXML(AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.DrawLineLayout, 0, 0);
        lineColor = ta.getColor(R.styleable.DrawLineLayout_lineColor,
                getResources().getColor(R.color.color_line));
        lineWidth = ta.getDimension(R.styleable.DrawLineLayout_lineWidth, 1);
        lineGravity = ta.getInt(R.styleable.DrawLineLayout_lineGravity,
                GRAVITY_BOTTOM);
        linePaddingTop = ta.getDimension(
                R.styleable.DrawLineLayout_lineMarginTop, 0);
        linePaddingBottom = ta.getDimension(
                R.styleable.DrawLineLayout_lineMarginBottom, 0);
        linePaddingLeft = ta.getDimension(
                R.styleable.DrawLineLayout_lineMarginLeft, 0);
        dealOnTouch = ta.getBoolean(R.styleable.DrawLineLayout_dealOnTouch,
                false);
    }

    /**
     * 初始化
     * @param attrs
     */
    private void init(AttributeSet attrs) {
        readXML(attrs);
        paint = new Paint();
        paint.setColor(lineColor);
        paint.setStrokeWidth(lineWidth);
        if (dealOnTouch) {
            setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            setLineColor(getResources()
                                    .getColor(R.color.color_theme));
                            break;

                        case MotionEvent.ACTION_UP:
                            setLineColor(lineColor);
                            break;
                    }
                    return false;
                }
            });
        }
    }

    public DrawLineTextView(Context context) {
        this(context, null);
    }

    /**
     * 谁在 margins
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setLineMargins(float left, float top, float right, float bottom) {
        this.linePaddingLeft = left;
        this.linePaddingTop = top;
        this.linePaddingRight = right;
        this.linePaddingBottom = bottom;
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        height = h;
        width = w;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        switch (lineGravity) {
            case GRAVITY_LEFT:
                drawLeftLine(canvas, height, width);
                break;
            case GRAVITY_RIGHT:
                drawRightLine(canvas, height, width);
                break;
            case GRAVITY_TOP:
                drawTopLine(canvas, height, width);
                break;
            case GRAVITY_BOTTOM:
                drawBottomLine(canvas, height, width);
                break;
            case GRAVITY_TOP_BOTTOM:
                drawTopLine(canvas, height, width);
                drawBottomLine(canvas, height, width);
                break;
            case GRAVITY_ALL:
                drawRect(canvas, height, width);
                break;
            case GRAVITY_LEFT_BOTTOM:
                drawLeftLine(canvas, height, width);
                drawBottomLine(canvas, height, width);
                break;
            case GRAVITY_LEFT_BOTTOM_RIGHT:
                drawLeftLine(canvas, height, width);
                drawBottomLine(canvas, height, width);
                drawRightLine(canvas, height, width);
                break;
            case GRAVITY_TOP_LEFT_BOTTOM:
                drawTopLine(canvas, height, width);
                drawBottomLine(canvas, height, width);
                drawLeftLine(canvas, height, width);
                break;
        }
        super.onDraw(canvas);
    }

    /**
     * 设置线条左侧pading
     * @param linePaddingLeft
     */
    public void setLinePaddingLeft(int linePaddingLeft) {
        this.linePaddingLeft = linePaddingLeft;
    }

    /**
     * 设置线条右侧pading
     * @param linePaddingRight
     */
    public void setLinePaddingRight(int linePaddingRight) {
        this.linePaddingRight = linePaddingRight;
    }

    /**
     * 顶部画线
     * @param canvas
     * @param height
     * @param width
     */
    public void drawTopLine(Canvas canvas, int height, int width) {
        canvas.drawLine(0, 0, width, 0, paint);
    }

    /**
     * 底部画线
     * @param canvas
     * @param height
     * @param width
     */
    public void drawBottomLine(Canvas canvas, int height, int width) {
        height -= lineWidth;
        canvas.drawLine(linePaddingLeft, height, width - linePaddingRight,
                height, paint);
    }

    /**
     * 左侧画线
     * @param canvas
     * @param height
     * @param width
     */
    public void drawLeftLine(Canvas canvas, int height, int width) {
        canvas.drawLine(0, linePaddingTop, 0, height - linePaddingBottom, paint);
    }

    /**
     *画右侧线
     * @param canvas
     * @param height
     * @param width
     */
    public void drawRightLine(Canvas canvas, int height, int width) {
        width -= lineWidth * 2;
        canvas.drawLine(width, linePaddingTop, width, height
                - linePaddingBottom, paint);
    }

    /**
     * 画矩形
     * @param canvas
     * @param height
     * @param width
     */
    public void drawRect(Canvas canvas, int height, int width) {
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(lineWidth, lineWidth, width - lineWidth * 2, height
                - lineWidth * 2, paint);
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    /**
     * 设置线的颜色
     * @param lineColor
     */
    public void setLineColor(int lineColor) {
        paint.setColor(lineColor);
        invalidate();
    }

    /**
     * 设置线的宽度
     * @param lineWidth
     */
    public void setLineWidth(int lineWidth) {
        paint.setStrokeWidth(lineWidth);
        invalidate();
    }

    /**
     * 重置线
     * @param lineColor
     * @param lineWidth
     */
    public void reDrawLine(int lineColor, int lineWidth) {
        paint.setStrokeWidth(lineWidth);
        paint.setColor(lineColor);
        invalidate();
    }

    /**
     * 设置位置
     * @param lineGravity
     */
    public void setLineGravity(int lineGravity) {
        this.lineGravity = lineGravity;
        invalidate();
    }
}
